package xiao_jin.fantastic_journey.core.capability;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import xiao_jin.fantastic_journey.FantasticJourney;
import xiao_jin.fantastic_journey.api.capability.Capability;
import xiao_jin.fantastic_journey.util.ModUtil;

public class ManaCapability extends Capability {
    public static final Identifier NAME = ModUtil.getIdOfMod("mana");

    public ManaCapability() {
        super(NAME);
    }

    private int manaValue = 0;
    private int maxManaValue = 100;

    public int getManaValue() {
        return manaValue;
    }

    public void setManaValue(int manaValue) {
        this.manaValue = MathHelper.clamp(manaValue, 0, maxManaValue);
    }

    public void addManaValue(int manaValue) {
        this.manaValue = MathHelper.clamp(this.manaValue + manaValue, 0, maxManaValue);
    }

    public int getMaxManaValue() {
        return maxManaValue;
    }

    public void setMaxManaValue(int maxManaValue) {
        this.maxManaValue = Math.max(maxManaValue, 0);
    }

    @Override
    public void init() {
        this.manaValue = 0;
        this.maxManaValue = 100;
    }

    @Override
    protected void readFromNBT(NbtCompound nbt) {
        this.setManaValue(!nbt.contains("mana") ? 0 : nbt.getInt("mana"));
        this.setMaxManaValue(!nbt.contains("mana_max") ? 100 : nbt.getInt("mana_max"));
    }

    @Override
    protected void writeToNBT(NbtCompound nbt) {
        nbt.putInt("mana", this.manaValue);
        nbt.putInt("mana_max", this.maxManaValue);
    }
}
